﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class panelAlertsMessage : UserControl
    {
        private static EmployeeItem empItem;
        public panelAlertsMessage()
        {
            InitializeComponent();
            this.grvAppointMent.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        public void setEmployee(EmployeeItem item)
        {
            empItem = item;
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                if (((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value != null && !((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value.ToString().Equals(string.Empty))
                {
                    frmAlertsMessage frmAlerts = new frmAlertsMessage(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()));
                    frmAlerts.ShowDialog();
                    refreshGridview();
                }
                else
                {
                    refreshGridview();
                }
            }
        }

        private void genGridview()
        {
            // gen Grideview
            grvAppointMent.AutoGenerateColumns = false;

            //Set Columns Count
            grvAppointMent.ColumnCount = 3;
            grvAppointMent.Columns[0].Width = 50;
            //Add Columns
            grvAppointMent.Columns[1].Name = "ALERTS_MESSAGE_ID";
            grvAppointMent.Columns[1].HeaderText = "รหัสการขอกู้";
            grvAppointMent.Columns[1].DataPropertyName = "ALERTS_MESSAGE_ID";
            grvAppointMent.Columns[1].Width = 90;

            grvAppointMent.Columns[2].Name = "ALERTS_MESSAGE_DETAIL";
            grvAppointMent.Columns[2].HeaderText = "รายละเอียด";
            grvAppointMent.Columns[2].DataPropertyName = "ALERTS_MESSAGE_DETAIL";
            grvAppointMent.Columns[2].Width = 350;

            // end gen gridview
        }

        public void refreshGridview()
        {
            grvAppointMent.DataSource = LoadAlertsMessage();
        }

        private DataTable LoadAlertsMessage()
        {
            DataTable dt = DAL.AlertsMessageInfoDAL.LoadAllAlertsMessageEmp(this.txtMessageId.Text,this.txtMessage.Text,empItem.USERNAME);
            return dt;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            grvAppointMent.DataSource = LoadAlertsMessage();
        }
    }
}
